/* Test source code to test libcurl.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <curl/curl.h>


struct MemoryStruct {
  char *memory;
  size_t size;
  int match;
};

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  mem->match = memcmp(mem->memory, contents, (int)realsize);
  if(mem->match == 0) return realsize;

  mem->memory = realloc(mem->memory, realsize + 1);
  if(mem->memory == NULL) {
    /* out of memory! */
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  memcpy(mem->memory, contents, realsize);
  mem->size = realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}


int main(void)
{
  CURL *curl_handle;
  CURLcode res;
  char* pd[40]; //the point of each ,
  int dn, pn; 


  struct MemoryStruct chunk;

  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
  chunk.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */
  curl_handle = curl_easy_init();

  /* specify URL to get */
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://hq.sinajs.cn/list=sz002121");

  /* send all data to this function  */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

  /* we pass our 'chunk' struct to the callback function */
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  
  curl_easy_setopt( curl_handle, CURLOPT_CONNECTTIMEOUT, 3 );
  curl_easy_setopt( curl_handle, CURLOPT_TIMEOUT, 10 );

  /* some servers don't like requests that are made without a user-agent
     field, so we provide one */
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
   
  
  while(1) {

  /* get it! */
  res = curl_easy_perform(curl_handle);

  /* check for errors */
  if(res != CURLE_OK) {
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
  }
  else {
    /*
     * Now, our chunk.memory points to a memory block that is chunk.size
     * bytes big and contains the remote file.
     *
     * Do something nice with it!
     */
     //if chunk.match == 0, the data not update
    if(!chunk.match) {printf("Not update!\n");sleep(5);continue;}
    
    //search the whole chunk.memory, if the value is "," then record 
    //the pointer to pd[pn]
    for(dn=0, pn=0; dn<chunk.size; dn++){
      if((*(chunk.memory + dn))== ','){
        pd[pn] = chunk.memory + dn;
        pn++;
      }
    }

    printf("%lu bytes retrieved\n", (long)chunk.size);
    printf("%d , found\n", pn);
    
    printf("股票代码：");
    for(dn = -18; dn < -10; dn++){
      printf("%c", *(pd[0] + dn));
    }
    printf("\n");
    
    pn = 0;
    printf("今开：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("昨收：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("当前：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("最高：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("最低：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("竞买：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("竞卖：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("成交手：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("成交额：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("买一：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("买二：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
     printf("买三：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
     printf("买四：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
     printf("买五：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
     printf("卖一：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("卖二：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("卖三：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("卖四：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("卖五：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf(" 价格 ");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("日期：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    pn++;
    printf("\n");
    
    printf("时间：");
    for(dn = 1; (pd[pn] + dn) < pd[pn+1]; dn++){
      printf("%c", *(pd[pn] + dn));
    }
    printf("\n");
    
    
  }
    sleep(5);
}

  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);

  if(chunk.memory)
    free(chunk.memory);

  /* we're done with libcurl, so clean it up */
  curl_global_cleanup();

  return 0;
}
